package com.xzx.shop_front_api.controller;

import com.xzx.shop_front_api.entity.ApiAccount;
import com.xzx.shop_front_api.service.ApiAccountService;
import com.xzx.shop_front_api.service.impl.ApiAccountServiceImpl;
import com.xzx.shop_front_api.utils.JsonResult;
import com.xzx.shop_front_api.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("api/api_account")
public class ApiAccountController {
    @Autowired
    private ApiAccountService apiAccountService;

    @PostMapping("register")
    public JsonResult register(@RequestBody ApiAccount account) {
        //验证参数
        String username = account.getUsername();
        String password = account.getPassword();
        if (username == null || password == null) {
            return JsonResult.failed("账号或密码不能为空");
        }
        //验证用户名不能重复
        ApiAccount accountInfo = apiAccountService.getAccountByUsername(username);
        if (accountInfo != null) {
            return JsonResult.failed("该账号已经存在，请重新输入.");
        }
        //新增账号
        //md5密码加密
        account.setPassword(Md5Utils.encrypt(password));
        int result = apiAccountService.insertAccount(account);
        if (result == 0) {
            return JsonResult.failed("注册失败.");
        }
        return JsonResult.success("注册成功", null);
    }
}
